﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DocumentFormat.OpenXml.Lite.Core.DocumentFS
{
    public class DocumentFS
    {
        public static Directory Root = new Directory("/", string.Empty);

        private static DocumentFS instance = new DocumentFS();

        private DocumentFS()
        {
        }

        public static bool Move(string source, string destination)
        {
            string sfile, sdirectory, dfile, ddirectory;
            bool s = ParseFile(source, out sfile, out sdirectory);
            bool d = ParseFile(destination, out dfile, out ddirectory);
            if (s && d)
            {
                Directory sdir = GetDirectory(sdirectory);
                Directory ddir = GetDirectory(ddirectory);

                File f = sdir.GetFile(source);
                if(sdir.RemoveFile(sfile))
                {
                    return ddir.CreateFile(dfile, f.Object) != null;
                }
                return false;
            }
            else if(!s && !d)
            {
                string[] sdirs = sdirectory.Split('/');
                string[] ddirs = ddirectory.Split('/');
                Directory sdir = GetDirectory(string.Join("/", sdirs.Take(sdirs.Length - 1)));
                Directory ddir = GetDirectory(string.Join("/", ddirs.Take(ddirs.Length - 1)));
                if(sdir.RemoveDirectory(sdirs.Last()))
                {
                    return ddir.CreateDirectory(ddirs.Last()) != null;
                }
                return false;
            }
            return false;
        }

        public static bool Exists(string path)
        {
            string file, directory;
            if (ParseFile(path, out file, out directory))
            {
                Directory dir = GetDirectory(directory);
                return dir.ExistsFile(file);
            }
            else
            {
                string[] dirs = directory.Split('/');
                Directory dir = GetDirectory(string.Join("/", dirs.Take(dirs.Length - 1)));
                return dir.ExistsDirectory(dirs.Last());
            }
        }

        public static File GetFile(string path)
        {
            string file, directory;
            if (ParseFile(path, out file, out directory))
            {
                Directory dir = GetDirectory(directory);
                return dir.GetFile(file);
            }
            return null;
        }

        public static Directory GetDirectory(string path)
        {
            string file, directory;
            if (!ParseFile(path, out file, out directory))
            {
                string[] dirs = directory.Split('/');
                Directory dir = GetDirectory(string.Join("/", dirs.Take(dirs.Length - 1)));
                return dir.GetDirectory(dirs.Last());
            }
            return null;
        }

        public static Guid CreateSandbox()
        {
            Guid id = Guid.NewGuid();
            Root.CreateDirectory(id.ToString());
            return id;
        }

        public static Directory GetSandbox(Guid id)
        {
            return Root.GetDirectory(id.ToString());
        }

        private static bool ParseFile(string path, out string directory, out string file)
        {
            file = string.Empty;
            directory = string.Empty;

            string pattern = @"/([a-zA-Z0-9/\-_]+)\.(([a-zA-Z0-9\-_\[\[]+)\.\w+)";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(path);
            if (match.Success)
            {
                file = match.Groups[1].Value;
                directory = match.Groups[2].Value;
                return true;
            }

            return false;
        }
    }
}
